// pages/commentdetail/commentdetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    hasMore:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var that = this;
    this.setData({
      id:id,
      USER_ID:wx.getStorageSync("USER_ID"),
    });

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/findleaving_MessageId',
      data:{
        LEAVING_MESSAGE_ID:id,
      },
      success:(res) => {
        var data = res.data;
        console.log(data);

        that.setData({
          obj:data.data
        })
      }
    });

    this.loadcomments()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },

  loadcomments:function(){
    var that = this;

    var id =this.data.id,
    page = this.data.page;
    wx.showLoading({
      title: '加载留言...',
      mask:true
    })
    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/findCommentleaving_Message',
      data:{
        LEAVING_MESSAGE_ID:id,
        pageNum:page
      },
      success:(res)=>{
        wx.hideLoading();
        var data =res.data;
        if(data.code==1){

          if(!data.data.object||data.data.object.length<=0){
            that.setData({
              hasMore:false,
            })
          }else{
            var list = that.data.list,
            newlist = [],
            l = data.data.object;
            if(that.data.page==1){
              for (var i = 0; i < l.length; i++) {
                newlist.push(l[i]);
              }
              that.setData({
                list: newlist,
                page:++that.data.page
              })
            }else{
              for (var i = 0; i < l.length; i++) {
                list.push(l[i]);
              }
              that.setData({
                list: list,
                page: ++that.data.page
              })
            }

          }
        }
      }
    })
  },

  comipt:function(e){
    this.setData({
      ipt:e.detail.value
    });
  },


  sendcom:function(){
    var val = this.data.ipt;
    var that = this;
    if(!val||val.length<=0){
      return false;
    }

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/saveCommentLeavingMessage',
      method:"POST",
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      data:{
        USER_ID:this.data.USER_ID,
        LEAVING_MESSAGE_ID:this.data.id,
        MESSAGE:val
      },
      success:function(res){
        var data = res.data;
        console.log(data);
        if(data.code==1){
          wx.showToast({
            title: '评论成功！',
          });
          that.setData({
            page:1,
            hasMore:true,
          });
          that.loadcomments();
        }
      }
    })
  }
})